package bll

import (
	"context"
	"encoding/json"
	"maya-service/store/redis"
	"maya-service/utils/request"
)

// GetUserInfoList 获取用户信息
func GetUserInfoList(ctx context.Context, userIds []string) (userMap map[string]*request.UserData, err error) {
	userMap = make(map[string]*request.UserData)
	result, err := redis.KV.HMGet(ctx, "quanxi:user_info", userIds...).Result()
	if err != nil {
		return
	}

	for i := 0; i < len(userIds); i++ {
		userData := &request.UserData{}
		userMap[userIds[i]] = userData
		v, ok := result[i].(string)
		if !ok {
			continue
		}
		err = json.Unmarshal([]byte(v), userData)
		if err != nil {
			continue
		}
	}
	return
}

// GetUserInfoById 获取用户信息
func GetUserInfoById(ctx context.Context, userId string) (user *request.UserData, err error) {
	userMap, err := GetUserInfoList(ctx, []string{userId})
	if err != nil {
		return &request.UserData{}, err
	}

	if v, ok := userMap[userId]; ok {
		return v, nil
	}

	return &request.UserData{}, nil
}
